package com.dong.controller;

import com.dong.entity.Video;
import com.dong.service.impl.VideoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private VideoServiceImpl videoService;

    @RequestMapping("/getallvideo")
    public String getAllVideo(Model model) {
        List<Video> videos = videoService.selectAllVideo();
        model.addAttribute("videoList", videos);
        return "allvideo";

    }

    @GetMapping("/toaddvideo")
    public String toAddPaper() {
        return "addvideo";
    }

    @PostMapping ("/addvideo")
    public String addVideo(Video video) {
        System.out.println(video);
        Integer integer = videoService.addVideo(video);
        return "redirect:/video/getallvideo";


    }


    @GetMapping("/toupdatevideo/{video_id}")
    public String toupdateVideo(Model model, @PathVariable("video_id") Integer id) {
        Video videos = videoService.selectVideoById(id);
        System.out.println(videos);
        model.addAttribute("videos", videos);
        return "updatevideo";

    }

    @PostMapping ("/updatevideo")
    public String updatevideo(Model model, Video video) {
        System.out.println(video);
        videoService.updateVideo(video);
        Video videos = videoService.selectVideoById(video.getVideo_id());
        model.addAttribute("videos", videos);
        return "redirect:/video/getallvideo";
    }
    @GetMapping("/del/{video_id}")
    public String deleteVideo(@PathVariable("video_id") int id) {
        videoService.deleteVideo(id);
        return "redirect:/video/getallvideo";
    }
}
